package xyz.bali16.module.netdisk.controller;

import com.gitee.starblues.bootstrap.annotation.AutowiredType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ModelAttribute;
import xyz.bali16.application.core.enums.ResponseEnum;
import xyz.bali16.application.core.model.Result;
import xyz.bali16.application.core.utils.AuthUtils;
import xyz.bali16.application.entity.User;
import xyz.bali16.application.service.UserService;
import xyz.bali16.module.netdisk.service.FileFolderService;
import xyz.bali16.module.netdisk.service.FileStorageService;
import xyz.bali16.module.netdisk.service.FileUserService;
import xyz.bali16.module.netdisk.service.UploadFileService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public abstract class BaseController {
    @Autowired
    protected UploadFileService uploadFileService;
    @Autowired
    protected FileFolderService fileFolderService;
    @Autowired
    protected FileStorageService fileStorageService;
    @Autowired
    protected FileUserService fileUserService;

    protected HttpServletRequest request;
    protected HttpServletResponse response;
    @Autowired
    @AutowiredType(AutowiredType.Type.MAIN_PLUGIN)
    UserService userService;
    protected User sysUser;

    public static final String API = "/api/";

    @ModelAttribute
    public void setReqAndRes(HttpServletRequest request, HttpServletResponse response) {
        this.request = request;
        this.response = response;
        User user = userService.getByUsername( AuthUtils.getCurrentUsername());
        sysUser = user;
    }

    /**
     * 返回接口封装 带有浏览器状态码的
     * @param responseEnum HttpStatusEnum 类中的枚举 注意状态码直接时枚举中的code, 定义的状态和浏览器规范的状态码必须一致
     * @param bodyEnum body 体中的枚举。
     * @param bodyMessage body 信息
     * @param bodyData body 返回的数据
     * @param <T> 返回数据的类型
     * @return ResponseEntity
     */
    protected <T> ResponseEntity<Result<T>> responseStatusAndBody(ResponseEnum responseEnum,
                                                                  ResponseEnum bodyEnum,
                                                                  String bodyMessage, T bodyData){
        Result<T> body = Result.<T>toBuilder()
                .result(bodyData)
                .responseEnum(bodyEnum)
                .message(bodyMessage)
                .builder();
        return ResponseEntity.status(responseEnum.getCode())
                .body(body);
    }

    /**
     * 返回接口封装 带有浏览器状态码的
     * @param responseEnum HttpStatusEnum 类中的枚举 注意状态码直接时枚举中的code, 定义的状态和浏览器规范的状态码必须一致
     * @param bodyEnum body 体中的枚举。
     * @param bodyMsg body体信息
     * @param <T> 返回数据的类型
     * @return ResponseEntity
     */
    protected <T> ResponseEntity<Result<T>> responseStatusAndBody(ResponseEnum responseEnum,
                                                                  ResponseEnum bodyEnum,
                                                                  String bodyMsg){
        return this.responseStatusAndBody(responseEnum, bodyEnum, bodyMsg, null);
    }

    /**
     * 返回接口封装 带有浏览器状态码的
     * @param responseEnum HttpStatusEnum 类中的枚举 注意状态码直接时枚举中的code, 定义的状态和浏览器规范的状态码必须一致
     * @param bodyEnum body 体中的枚举。
     * @param <T> 返回数据的类型
     * @return ResponseEntity
     */
    protected <T> ResponseEntity<Result<T>> responseStatusAndBody(ResponseEnum responseEnum, ResponseEnum bodyEnum){
        return this.responseStatusAndBody(responseEnum, bodyEnum, bodyEnum.getMessage(), null);
    }



}
